package com.newbingo.action;

import java.util.Date;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.googlecode.jsonplugin.annotations.JSON;
import com.newbingo.dto.AgentDto;
import com.newbingo.dto.MemberDto;
import com.newbingo.entity.Agent;
import com.newbingo.entity.BettingItem;
import com.newbingo.entity.Game;
import com.newbingo.entity.Member;
import com.newbingo.service.imple.IGameService;
import com.newbingo.service.imple.IMemberService;
import com.newbingo.util.Pager;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

/**
 * 会员Action
 * 
 * @author ChansonYan
 * 
 */
public class MemberAction extends ActionSupport {
	// Spring注入属性
	private IMemberService memberService = null;

	/*
	 * 分页查询条件
	 */
	private Pager pager = null;
	private int start;
	private int limit = 10;
	private String uname;
	private Integer agentId;

	// 根据游戏编号，投注时间查询的传参
	private Date startTime;
	private Date endTime;
	private String gameName;

	// 操作类
	private BettingItem betItem;
	private Member member;

	// 修改密码传值
	private String oldPwd;
	private String newPwd;

	/*
	 * 修改会员状态，member.uno和member.status(String)传入修改会员状态 添加新会员，将member中的基本信息传入
	 */
	private boolean success = false;

	/*
	 * 根据会员登录名和代理的ID进行模糊查询，进行分页显示
	 */
	public String findByPage() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (session.getAttribute("user") != null) {
			AgentDto agent = (AgentDto) session.getAttribute("user");
			// pager对象中的data为MeberDto的泛型
			pager = memberService.findMembersByPage(start, limit, uname, agent
					.getUno());
			success = true;
		} else {
			success = false;
		}
		return SUCCESS;
	}

	/**
	 * 
	 * @return
	 */
	public String updatePassword() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		MemberDto memberDto = (MemberDto) session.getAttribute("member");
		Member member = memberService.findByMemberId(memberDto.getUno());
        if (oldPwd!=null && newPwd!=null) {
        	if (oldPwd.equals(member.getUpwd())) {
    			member.setUpwd(newPwd);
    			memberService.updateMember(member);
    			success=true;				
			}else{
				success=false;	
			}
		}else {
			success=false;
		}
        return SUCCESS;
	}

	/*
	 * 查询历史投注记录
	 */
	public String findHistoryBetByMemberId() {
		this.setPager(memberService.findByMemberId(start, limit, member
				.getUno(), this.getGameName(), startTime, endTime));
		success = true;
		return SUCCESS;
		// return "history";
	}


	
	/*
	 * 根据会员ID进行删除
	 */
	public String deleteMember() {
		memberService.deleteMember(member);
		System.out.println(member.getUno());
		success = true;
		return SUCCESS;
	}

	/*
	 * 创建新会员
	 */
	public String createNewMember() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		AgentDto agentDto = (AgentDto) session.getAttribute("user");
		member.setStatus("正常");
		member.getAgent().setUno(agentDto.getUno());
		memberService.createNewMember(member);
		return SUCCESS;
	}

	/*
	 * 修改会员信息，供会员进行修改个人基本信息
	 */
	public String updateMember() {
		memberService.updateMember(member);
		success = true;
		return SUCCESS;
	}

	/*
	 * 修改会员状态，供后台操作员进行会员状态修改
	 */
	public String updateMemberStatus() {
		memberService.updateMemberStatus(member);
		success = true;
		return SUCCESS;
	}

	public Pager getPager() {
		return pager;
	}

	public void setPager(Pager pager) {
		this.pager = pager;
	}

	@JSON(serialize = false)
	public IMemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(IMemberService memberService) {
		this.memberService = memberService;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public Integer getAgentId() {
		return agentId;
	}

	public void setAgentId(Integer agentId) {
		this.agentId = agentId;
	}

	@JSON(serialize = false)
	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public BettingItem getBetItem() {
		return betItem;
	}

	public void setBetItem(BettingItem betItem) {
		this.betItem = betItem;
	}

	public Date getStartTime() {
		return startTime;
	}

	public void setStartTime(Date startTime) {
		this.startTime = startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public void setEndTime(Date endTime) {
		this.endTime = endTime;
	}

	public String getGameName() {
		return gameName;
	}

	public void setGameName(String gameName) {
		this.gameName = gameName;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

}
